package com.etlmaster.executor.steps.web;


import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.StepState;

/**
 * @author Wugf
 * @date 2016-03-08
 */
@StepInstruction("callService")
public class CallWebServiceStep extends Step {

	private CallWebServiceStepMeta meta;

	public CallWebServiceStep(StepMeta meta) {
		super(meta);
		this.meta = (CallWebServiceStepMeta) meta;
	}

	@Override
	protected int handle() {
		RestClient rc = new RestClient(meta.getUrl());
		String result = "";
		int flag = StepState.RUNNING_SUCCESSED;
		meta.log("INFO","调用服务:{},参数:{}",meta.getUrl(),meta.getParam());

		try {
			if (meta.getType().equalsIgnoreCase("get")) {
				result = rc.show(meta.getParam());
			} else if (meta.getType().equalsIgnoreCase("put")) {
				result = rc.put();
			} else if (meta.getType().equalsIgnoreCase("post")) {
				result = rc.post();
			} else if (meta.getType().equalsIgnoreCase("delete")) {
				result = rc.delete();
			}else{
				meta.log("ERROR","不支持的rest类型：{}",meta.getType());
				flag = StepState.RUNNING_FAILED;

			}
			meta.log("INFO",result);
		} catch (Exception e) {
			meta.log("ERROR","调用服务异常：{}", e.getMessage());
			result = "调用服务异常：" + e.getMessage();
			flag = StepState.RUNNING_FAILED;
		}
		meta.getStepState().getReturnValue().put("logInfo", result);
		return flag;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
